
package com.example.project.activity.wode;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.VolleyError;
import com.example.project.MyApplication;
import com.example.project.R;
import com.example.project.activity.mingxi.MainActivity;
import com.example.project.activity.tubiao.MonthChartActivity;
import com.example.project.activity.jizhang.RecordActivity;
import com.example.project.activity.kaishi.LoginActivity;
import com.example.project.bean.AccountBean;
import com.example.project.dialog.BudgetDialog;
import com.example.project.tool.SharePreferce;
import com.example.project.utils.DateUtil;
import com.example.project.utils.MonPickerDialog;
import com.example.project.utils.YearPickerDialog;
import com.example.project.volley.ConstantUtil;
import com.example.project.volley.Params;
import com.example.project.volley.VolleyInterface;
import com.example.project.volley.VolleyRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import static com.example.project.tool.SharePreferce.getInstance;

//我的页面
public class WdActivity extends AppCompatActivity implements View.OnClickListener {
    //下方导航条
    private ImageView Ivjz,Ivbb,Ivzj,Ivwd;
    private TextView tvwd,tvName,tvTT; //下方的名字，名称
    private ImageView ivHead; //头像

    private Button btBudget,btSort,btRemind,btPass,btExport,btSync,btQuit; //退出登录
    int year,month,day,kind,mainId; //年，月，日
    String years,months,days,kinds,mainIds;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_wd );
        initView();
        setInitTime();
        volleyUserInfo(mainIds);
    }
    //获取当前时间，显示出来
    private void setInitTime() {
        Date date=new Date(  );  //实例化时间
        SimpleDateFormat sdf=new SimpleDateFormat( "yyyy年MM月dd日 HH:mm" ); //设置时间格式
        String time=sdf.format( date ); //加载时间格式

        Calendar calendar=Calendar.getInstance(); //实例化日历
        year=calendar.get( calendar.YEAR ); //获取日历的年
        month=calendar.get( calendar.MARCH )+1; //获取日历的月
        day=calendar.get( calendar.DAY_OF_MONTH ); //获取日历的天
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    //实例化
    private void initView() {
        mainId= MyApplication.getMainId(); //获取用户信息
        mainIds=mainId+"";
        Ivjz = findViewById( R.id.Iv_jz );
        Ivjz.setOnClickListener( this );
        Ivbb = findViewById( R.id.Iv_bb );
        Ivbb.setOnClickListener( this );
        Ivzj = findViewById( R.id.Iv_zj );
        Ivzj.setOnClickListener( this );
        Ivwd = findViewById( R.id.Iv_wd );
        Ivwd.setOnClickListener( this );
        tvwd= findViewById( R.id.tv_wd );
        ivHead=findViewById( R.id.iv_head );
        ivHead.setOnClickListener( this );
        tvName=findViewById( R.id.tv_name );
        tvTT=findViewById( R.id.tv_TT );

        btBudget=findViewById( R.id.bt_budget );
        btBudget.setOnClickListener( this );
//        btSort=findViewById( R.id.bt_sort );
//        btSort.setOnClickListener( this );
        btRemind=findViewById( R.id.bt_remind );
        btRemind.setOnClickListener( this );
        btPass=findViewById( R.id.bt_pass );
        btPass.setOnClickListener( this );
        btExport=findViewById( R.id.bt_export );
        btExport.setOnClickListener( this );
        btSync=findViewById( R.id.bt_sync );
        btSync.setOnClickListener( this );
        btQuit=findViewById( R.id.bt_quit );
        btQuit.setOnClickListener( this );

        mainId= MyApplication.getMainId(); //获取用户信息

        String id= getInstance(WdActivity.this).getString("userID");
        System.out.println( "Id"+id );
        if(!id.equals( "" )){
            int head= getInstance(WdActivity.this).getInt("head"); //
            ivHead.setImageResource( R.drawable.h1 );
            String name= getInstance(WdActivity.this).getString("userID");
            tvName.setText( name );
        }

        Ivwd.setImageResource( R.drawable.wdb);
        tvwd.setTextColor( Color.parseColor("#87CEFA") );
    }

    //退出登录
    private void logOut(){
        File file= new File("/data/data/"+getPackageName().toString()+"/shared_prefs","QQ.xml");
        if(file.exists()){
            file.delete();
            Toast.makeText(this, "退出成功", Toast.LENGTH_LONG).show();
        }
    }

    /* 点击事件*/
    public void onClick(View view) {
        Intent intent=null;
        switch (view.getId()) {
            case R.id.Iv_jz:
                intent=new Intent(this, MainActivity.class);
                startActivity( intent );
                break;
            case R.id.Iv_bb:
                intent=new Intent(this, MonthChartActivity.class);
                startActivity( intent );
                break;
            case R.id.Iv_zj:
                intent=new Intent(this, RecordActivity.class);
                startActivity( intent );
                break;
            case R.id.iv_head:
                intent=new Intent( this,UserInfoActivity.class );
                startActivity( intent );
                break;
            case R.id.bt_export:
                intent=new Intent(this, ExportActivity.class);
                startActivity( intent );
                break;
            case R.id.bt_remind:
                intent=new Intent(this, RemindActivity.class);
                startActivity( intent );
                break;
            case R.id.bt_budget:
                showBudgetDialog();
                break;
            case R.id.bt_sync:
                Toast.makeText(WdActivity.this,"同步成功",Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt_quit:
                logOut();
                intent=new Intent( this, LoginActivity.class );
                startActivity( intent );
                break;

        }
    }

    /** 显示预算设置对话框*/
    private void showBudgetDialog() {
        BudgetDialog dialog = new BudgetDialog(this); //实例化对话框
        dialog.show(); //显示对话框
        dialog.setDialogSize(); //动态的设置弹出框大小的方法
        dialog.setOnEnsureListener(new BudgetDialog.OnEnsureListener() {
            @Override
            public void onEnsure(float money) {
                String moneys=String.valueOf(money);
                volleyUpdateMoney(mainIds,moneys);
            }
        });
    }

    //设置预算网络请求
    public void volleyUpdateMoney(String mainIds,String money) {
        VolleyRequest.RequesetPost(ConstantUtil.UPDATEMONEY, "UpdateMoney", Params.GetString2(mainIds,money), new VolleyInterface(this) {

            @Override
            public void onMySuccess(String result) {
                if(!result.equals("1")){
                    Toast.makeText(WdActivity.this, "设置失败", Toast.LENGTH_LONG).show();
                }
                Toast.makeText(WdActivity.this,"设置成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onMyError(VolleyError error) {
                Toast.makeText(WdActivity.this,"网络不好！！！",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onImageSuccess(Bitmap bitmap) {

            }
        });

    }

    //返现数据
    public void volleyUserInfo(String mainIds) {
        VolleyRequest.RequesetPost(ConstantUtil.USERINFO, "UserInfo", Params.GetString(mainIds), new VolleyInterface(this) {

            @Override
            public void onMySuccess(String result) throws JSONException {
                System.out.println("result:"+result);
                JSONObject jsonObject = null;
                jsonObject =new JSONObject( result );
                String username=jsonObject.getString( "username" );
                String img=jsonObject.getString( "img" );
                tvName.setText(username);
                tvTT.setText(mainIds);
            }

            @Override
            public void onMyError(VolleyError error) {
                Toast.makeText(WdActivity.this,"网络不好！！！",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onImageSuccess(Bitmap bitmap) {

            }
        });

    }


}